var qr = require('qr-image');

module.exports = {
    // 成功提示
    apiSuccess(data = '', msg = "ok", code = 200) {
        this.body = { msg, data }
        this.status = code
    },
    apiFail(data = "", msg = "fail", code = 400) {
        this.body = { msg, data }
        this.status = code
    },
    // 生成token
    getToken(value) {
        return this.app.jwt.sign(value, this.app.config.jwt.secret);
    },
    // 验证token
    checkToken(token) {
        return this.app.jwt.verify(token, this.app.config.jwt.secret);
    },
    // 生成唯一 code  10-~~
    getCode(length) {
        return Number(((new Date()).getTime() + Math.random().toString().substr(2, 10)).substr(0, length));
    },
    // 分页
    async page(model, where, options = {}) {
        let page = this.query.page ? parseInt(this.query.page) : 1;
        let limit = this.query.limit ? parseInt(this.query.limit) : 10;
        let offset = (page - 1) * limit;

        if (!options.order) {
            options.order = [
                ['id', 'DESC']
            ];
        }
        return await model.findAndCountAll({
            where,
            offset,
            limit,
            ...options
        });
    },
    //二维码
    qrcode(listOne) {
        let image = qr.image(JSON.stringify(listOne), { size: 10 });//size 图片大小  data 要生成的数据string类型
        this.response.type = 'image/png';  //图片格式
        // console.log('image--', image)
        this.body = image
    }
}